package com.feng.study.demo;

import com.vip.vjtools.vjkit.number.RandomUtil;

import java.security.SecureRandom;
import java.util.Random;

/**
 * 随机数生成，下面是两种方式生成随机数
 * vjtools工具生成，效率更高，方法更多（可以选择区间等等）
 */
public class TestRandom {
    public static void main(String[] args) {
        test();
//        demo001();
    }

    private static void demo001(){
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
//            SecureRandom secureRandom = RandomUtil.secureRandom();//慢得多，安全应该是加锁了吧
//            int i1 = RandomUtil.nextInt(RandomUtil.secureRandom());
//            Random random = RandomUtil.threadLocalRandom();//更快，无锁的ThreadLocalRandom
            int anInt = RandomUtil.nextInt(RandomUtil.threadLocalRandom());
        }
        long time = System.currentTimeMillis() - begin;
        System.out.println("vip.vjtools.vjkit.number.RandomUtil耗时：" + time);


        Random random = new Random();
        long begin2 = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            int anInt = random.nextInt();
        }
        long time2 = System.currentTimeMillis() - begin2;
        System.out.println("java.util.Random耗时：" + time2);
    }
    private static void test(){
        Math.random();
        int left=5;
        int right=10;
        Random random = new Random();
        //生成5-10的随机数，包括5和10
        while (true){
            int a=random.nextInt(right-left+1)+left;
            System.out.println(a);
        }
    }

}
